package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import jdk.net.SocketFlow;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo")CustomerQuery qo) {//创建qo对象
        //只查询潜在客户的状态
        qo.setStatus(Customer.STATUS_COMMON);

        //如果登录的是管理员或者是经理,可以查看所有的客户信息,如果是普通的销售人员,只能看看自己的客户,不能看别人的
        //shiro获取后登录信息
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //获取当前登录的用户
            Employee employee = (Employee) subject.getPrincipal();
            //只能是获取到当前员工id查询所跟进的客户
           qo.setSellerId(employee.getId());
        }

        //根据角色编码查询拥有该角色的员工Market Market_Manager
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees",employees);

        //职业下拉框的
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByDicSn("job");
        model.addAttribute("jobs",jobs);

        //来源下拉框数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByDicSn("source");
        model.addAttribute("sources",sources);

        //跟进交流方式下拉框数据,明细表中sn找到communicationMethod
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByDicSn("communicationMethod");
        model.addAttribute("ccts",ccts);



        PageInfo pageInfo = customerService.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/potentialList";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")CustomerQuery qo) {//创建qo对象
        //只查询潜在客户的状态
        //qo.setStatus(Customer.STATUS_COMMON);

        //如果登录的是管理员或者是经理,可以查看所有的客户信息,如果是普通的销售人员,只能看看自己的客户,不能看别人的
        //shiro获取后登录信息
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //获取当前登录的用户
            Employee employee = (Employee) subject.getPrincipal();
            //只能是获取到当前员工id查询所跟进的客户
            qo.setSellerId(employee.getId());
        }
        //根据状态下拉框查询
        List<Customer> status = customerService.listAll();
        model.addAttribute("status",status);

        //根据角色编码查询拥有该角色的员工Market Market_Manager
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees",employees);

        //职业下拉框的
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByDicSn("job");
        model.addAttribute("jobs",jobs);

        //来源下拉框数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByDicSn("source");
        model.addAttribute("sources",sources);


        PageInfo pageInfo = customerService.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/list";
    }



    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"saveOrUpdate:list","部门保存/更新"}, logical=Logical.OR)//or只要有一个就可以运行
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {

        if (customer.getId() == null) {
            customerService.save(customer);
        } else {
            customerService.update(customer);
        }
        return new JsonResult();

    }

}
